package com.tianma315.core.procedures.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;


import com.google.common.base.Supplier;
import com.tianma315.core.common.base.CoreServiceImpl;
import com.tianma315.core.constant.StateEnum;
import com.tianma315.core.oss.service.FileService;
import com.tianma315.core.procedures.dao.LinkContentDao;
import com.tianma315.core.procedures.domain.LinkContentDO;
import com.tianma315.core.procedures.domain.dto.LinkContentDto;
import com.tianma315.core.procedures.service.LinkContentService;


import com.tianma315.core.wx.domain.ImgValueDo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * 
 * <pre>
 * 环节内容表
 * </pre>
 * <small> 2019-08-13 16:34:19 | Lgc</small>
 */
@Service
public class LinkContentServiceImpl extends CoreServiceImpl<LinkContentDao, LinkContentDO> implements LinkContentService {

    @Autowired
    FileService fileService;
    /**
     *
     * @param pageNumber 当前页
     * @param pageSize  页的条数
     * @param linkContentDTO
     * @return 返回当前页的相应数据
     */
    @Override
    public Page<LinkContentDO> getLinkContentPage(Integer pageNumber, Integer pageSize, LinkContentDO linkContentDTO) {
        EntityWrapper<LinkContentDO> wrapper = new EntityWrapper<>(linkContentDTO);
        wrapper.eq("state", StateEnum.not_del);
        wrapper.eq("procedures_link_id",linkContentDTO.getProceduresLinkId());
        //wrapper.orderBy("sort",false);
        Page<LinkContentDO> page = new Page<>(pageNumber,pageSize);
        page = selectPage(page,wrapper);
        return page;
    }

    @Override
    public Boolean addLinkContent(LinkContentDto linkContentDO) {
        linkContentDO.setCreateDate(new Date());
        linkContentDO.setState(StateEnum.not_del.getCode());
        //添加图片逻辑
        ImgValueDo[] imgValueDos = linkContentDO.getImgValues();
        if (imgValueDos!=null){
            String urls = "";
            for (ImgValueDo imgValueDo : imgValueDos) {
                urls += fileService.uploadBase64(imgValueDo.getUrl(),imgValueDo.getSize())+",";
            }
            linkContentDO.setDefaultValue(urls.substring(0,urls.length()-1));
        }
        Boolean result = insert(linkContentDO);
        return result;
    }

    @Override
    public Boolean deleteLinkContent(Integer id) {
        LinkContentDO linkContentDO = new LinkContentDO();
        linkContentDO.setLinkContentId(id);
        linkContentDO.setState(StateEnum.is_del.getCode());
        Boolean result = updateById(linkContentDO);
        return result;
    }

    @Override
    public List<LinkContentDO> getLinkContentList(LinkContentDto linkContentDTO) {
        EntityWrapper<LinkContentDO> wrapper = new EntityWrapper<>();
        wrapper.eq("state", StateEnum.not_del);
        wrapper.eq("procedures_link_id",linkContentDTO.getProceduresLinkId());
        wrapper.eq("sort",linkContentDTO.getSort());
        if (linkContentDTO.getLinkContentId() !=null){
            wrapper.ne("link_content_id",linkContentDTO.getLinkContentId());
        }
        List<LinkContentDO> list = selectList(wrapper);
        return list;
    }

    @Override
    public List<LinkContentDO> getLinkContentListByProcedureLinkId(Integer procedureLinkId) {
        EntityWrapper<LinkContentDO> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("state",StateEnum.not_del.getCode());
        entityWrapper.eq("procedures_link_id",procedureLinkId);
        List<LinkContentDO> LinkContentDOList = selectList(entityWrapper);
        return LinkContentDOList;
    }

    @Override
    public List<LinkContentDO> selectDoList(Integer id) {
        Wrapper<LinkContentDO> wrapper = new EntityWrapper<LinkContentDO>();
        wrapper.eq("procedures_link_id",id);
        wrapper.eq("state", StateEnum.not_del);
        wrapper.orderBy("sort");
        List<LinkContentDO> linkContentDOS = selectList(wrapper);
        linkContentDOS = linkContentDOS.stream().map(dos->{
            if (dos.getType()==1) {
                String defaultValue = dos.getDefaultValue();
                if (StringUtils.isNotBlank(dos.getDefaultValue())) {
                    String[] split = defaultValue.split(",");
                    dos.setPics(Arrays.asList(split));
                }
            }
            return dos;
        }).collect(Collectors.toList());
        return linkContentDOS;
    }
}
